package src; 

import java.awt.Color;
import java.util.concurrent.*;

import src.GridLocation;
import src.Player;
import src.SuperTicTacToeGame;

/**
 * Represents a HumanPlayer. Additional "name" field added on top of existing functionality inherited from Player superclass. 
 * @author Akshat
 *
 */
public class HumanPlayer extends Player
{
	private Semaphore lock; 
	private String name; 
	
	public HumanPlayer(String n, Color c)
	{
		super(c);
		
		name = n;
		lock = new Semaphore(0); 
	}
	
	/**
	 * @return name of this player
	 */
	public String getName()
	{
	    return name;
	}

	/**
	 * LOCKED UNTIL setPlay is called (in Game after location clicked on GUI)
	 */
	public GridLocation getPlay()
	{	
		try
		{
			lock.acquire();
			return playLoc; 
		}
		catch( InterruptedException e )
		{
			System.out.println("FATAL INTERRUPTION EXCEPTION");
			throw new RuntimeException("FATAL INTERRUPTION EXCEPTION");
		}
	}
	
	/** 
	 * Called by the Game after the GUI has been clicked. Allows for getPlay() to be called once, which allows Player to Play() once
	 * allowing game to progress. 
	 * @param loc where to set play
	 */
	public void setPlay(GridLocation loc)
	{
		lock.drainPermits();
		playLoc = loc; 
		lock.release();
	}

	@Override
	public String toString() 
	{
		return getName();
	}
	
	
}
